# !/usr/bin/env python
# -*- coding:utf-8 -*-

import smtplib
from email.header import Header
from email.mime.text import MIMEText

from loguru import logger


def get_html(datas):
    header = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>'
    # https://xueqiu.com/S/SZ001234
    th = f"<body text='#000000'><table bgcolor='#B0E0E6' style=' font-size: 10px;'border='1' cellspacing='0' cellpadding='0' bordercolor='#000000' width='100%' align='center' >" \
         "<tr  bgcolor='#F79646' align='left' >" \
         f"<th style='width:200px'>行业</th>" \
         f"<th style='width:250px'>名称代码</th>" \
         f"<th style='width:200px'>累涨幅</th>" \
         f"<th style='width:200px'>今涨幅</th>" \
         f"<th style='width:200px'>detail</th>" \
         "</tr>"
    tr = ""
    # 谷底日期,谷底价,谷底跌幅
    index = 0
    len_cha_pre = ''
    bg_color_arr = ['#FFFFFF', '#B0E0E6']
    color_index = 0
    # len_cha_set = set()
    for index, item in enumerate(datas[1:]):
        sname = item.get('名称')
        snumber = item.get('代码')
        indus = item.get('行业')
        t_pct = item.get('今日涨幅')
        all_pct = item.get('累计涨幅')
        len_cha = str(item.get('len_cha'))
        # down_up_str = item.get('down_avg5')
        today_price = item.get('收盘价')
        today_date = item.get('日期')
        # len_cha_set.add(len_cha)
        if index > 10:
            break
        color_index = (~color_index) if (len_cha != len_cha_pre) else color_index
        bg_color = bg_color_arr[color_index]
        len_cha_pre = len_cha
        detail_info = (f'日：{today_date}'
                       f'<br>{today_price}元'
                       f'<br>次：{len_cha}'
                       f'<br>'
                       )
        stock_number_arr = snumber.split('.')
        stock_number = f'{stock_number_arr[1]}{stock_number_arr[0]}'
        h_a = f"<a href='https://xueqiu.com/S/{stock_number}'>{stock_number}<br>{sname}</a>"
        td = ''
        td = td + f"<td style='background-color:{bg_color};'>" + str(indus) + "</td>"
        td = td + f"<td style='background-color:{bg_color};'>" + str(h_a) + "</td>"
        td = td + f"<td style='background-color:{bg_color};'>" + str(all_pct) + "</td>"
        td = td + f"<td style='background-color:{bg_color};'>" + str(t_pct) + "</td>"
        td = td + f"<td style='background-color:{bg_color};'>" + str(detail_info) + "</td>"
        tr = tr + "<tr>" + td + "</tr>"

    body = str(tr)
    tail = f'</table></body></html>'
    html_txt = header + th + body + tail
    return index, html_txt


def send_html(datas, title):
    # 邮箱服务器地址
    # 发件人邮箱地址
    # 发件人邮箱密码(使用申请的客户端授权码代替)
    smtp_server = 'smtp.qq.com'
    from_addr = '83852084@qq.com'
    # passwd = 'ruqbqiqjmnjhbgih'
    passwd = 'iqjzamocyiaibjfg'

    # 收件人邮箱地址
    to_addr = 'stockbing@icloud.com'

    # 构建邮件内容(参数:邮件内容; 类型-plain,html; 编码)
    # msg = MIMEText(str(data), 'plain', 'utf-8')
    cnt, html = get_html(datas)
    msg = MIMEText(html, _subtype='html', _charset='utf-8')

    # 设置邮件主题
    title = f'{title}_{cnt}/{len(datas) - 1}'
    msg['Subject'] = Header(title, charset='utf-8')
    msg['from'] = from_addr
    msg['to'] = to_addr

    # 获取SMTP对象
    # server = smtplib.SMTP(host=smtp_server, port=25)
    server = smtplib.SMTP_SSL(host=smtp_server, port=465)
    # 登录163邮箱服务器
    server.login(user=from_addr, password=passwd)
    # 发送邮件
    server.sendmail(from_addr=from_addr, to_addrs=to_addr, msg=msg.as_string())
    # 退出邮箱服务器
    server.quit()
    logger.info(f'{title} 邮件发送成功')
